package net.biancheng.controller;

import java.util.List;
import net.biancheng.common.core.BaseController;
import net.biancheng.common.core.CommonResult;
import net.biancheng.common.core.bean.TreeSelect;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import net.biancheng.common.annotation.Log;
import net.biancheng.common.enums.BusinessType;
import net.biancheng.bean.SysDept;
import net.biancheng.service.ISysDeptService;

/**
 * 部门Controller
 * 
 * @author lb
 * @date 2024-01-25
 */
@RestController
@RequestMapping("/system/dept")
public class SysDeptController extends BaseController
{
    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 查询部门列表
     */
    @PreAuthorize("@ss.hasPermi('system:dept:list')")
    @GetMapping("/list")
    public CommonResult list(SysDept sysDept)
    {
        List<SysDept> list = sysDeptService.selectSysDeptList(sysDept);
        return CommonResult.success(list);
    }

    /**
     * 查询部门树形列表
     */
    @PreAuthorize("@ss.hasPermi('system:dept:treeList')")
    @GetMapping("/treeList")
    public CommonResult treeList(SysDept sysDept)
    {
        List<SysDept> list = sysDeptService.selectSysDeptList(sysDept);
        List<SysDept> treeList = sysDeptService.buildDeptTree(list);
        return CommonResult.success(treeList);
    }

    /**
     * 查询部门下拉树选择列表
     */
    @PreAuthorize("@ss.hasPermi('system:dept:treeSelect')")
    @GetMapping("/treeSelect")
    public CommonResult treeSelect(SysDept sysDept)
    {
        List<SysDept> list = sysDeptService.selectSysDeptList(sysDept);
        List<TreeSelect> treeSelect = sysDeptService.buildDeptTreeSelect(list);
        return CommonResult.success(treeSelect);
    }

    /**
     * 获取部门详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:dept:query')")
    @GetMapping(value = "/{id}")
    public CommonResult getInfo(@PathVariable Long id)
    {
        return CommonResult.success(sysDeptService.selectSysDeptById(id));
    }

    /**
     * 新增部门
     */
    @PreAuthorize("@ss.hasPermi('system:dept:add')")
    @Log(title = "部门", businessType = BusinessType.INSERT)
    @PostMapping
    public CommonResult add(@RequestBody SysDept sysDept)
    {
        return toResult(sysDeptService.insertSysDept(sysDept));
    }

    /**
     * 修改部门
     */
    @PreAuthorize("@ss.hasPermi('system:dept:edit')")
    @Log(title = "部门", businessType = BusinessType.UPDATE)
    @PutMapping
    public CommonResult edit(@RequestBody SysDept sysDept)
    {
        return toResult(sysDeptService.updateSysDept(sysDept));
    }

    /**
     * 删除部门
     */
    @PreAuthorize("@ss.hasPermi('system:dept:remove')")
    @Log(title = "部门", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public CommonResult remove(@PathVariable Long id)
    {
        return toResult(sysDeptService.deleteSysDeptById(id));
    }
}
